package com.sm.scorpio.ui.activity.login;

import android.Manifest;
import android.os.Bundle;

import com.scorpio.baselibrary.PoorApplication;
import com.scorpio.baselibrary.utils.AppDataUtil;
import com.scorpio.baselibrary.utils.permission.OnPermission;
import com.scorpio.baselibrary.utils.permission.ScorpioPermissions;
import com.sm.filemanagelibrary.util.FileManage;
import com.sm.imlibrary.imsdk.SIMManager;
import com.sm.imlibrary.nio.LoginCallbackListener;
import com.sm.scorpio.R;
import com.sm.scorpio.http.HttpNormalCallback;
import com.sm.scorpio.http.HttpUtils;
import com.sm.scorpio.ui.activity.MainActivity;
import com.sm.scorpio.ui.base.BaseActivity;
import com.sm.scorpio.ui.bean.model.UserBean;
import com.sm.scorpio.ui.bean.http.UserDataHttpBean;

import java.util.List;

public class WelcomeActivity extends BaseActivity {
    @Override
    protected int onCreateLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        hideStatusBar();
        hideToolbarView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        ScorpioPermissions.with(this)
                .permission(Manifest.permission.READ_EXTERNAL_STORAGE)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll) {
                            FileManage.open2(WelcomeActivity.this);
                            finish();
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        PoorApplication.getInstance().showToast(WelcomeActivity.this, getString(com.sm.chatinputlibrary.R.string.string_900126));
                        if (quick) {
                            ScorpioPermissions.gotoPermissionSettings(WelcomeActivity.this, true);
                        }
                    }
                });
//        UserBean userBean = (UserBean) AppDataUtil.getInstance().readData(new UserBean());
//        if (TextUtils.isEmpty(userBean.accountToken)) {
//            skipActivityFinish(LoginActivity.class);
//            return;
//        }
//        http();
    }

    private void http() {
        HttpUtils.postString().findUserInfo().enqueue(new HttpNormalCallback<UserDataHttpBean>(this) {
            @Override
            protected void onSuccess(UserDataHttpBean data) {
                UserBean userDataBean = (UserBean) AppDataUtil.getInstance().readData(new UserBean());
                userDataBean.nickname = data.data.nickname;
                userDataBean.sex = data.data.sex;
                AppDataUtil.getInstance().commitData(userDataBean);
                SIMManager.getInstance().login(userDataBean.accountToken, new LoginCallbackListener() {
                    @Override
                    public void onSucceed() {
                        skipActivityFinish(MainActivity.class);
                    }

                    @Override
                    public void onFail() {
                        skipActivityFinish(LoginActivity.class);
                    }
                });
            }

            @Override
            protected void onFail(int code, String url, String msg) {
                skipActivityFinish(MainActivity.class);
            }
        });
    }
}
